
//
//  YuYueCustomView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/9.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  控制器瘦身

#import "YuYueCustomView.h"

//view
#import "YuYueOrderHeaderView.h" //150 + 5 +167 
#import "WSSureOrderFooterView.h" //复用
#import "YuYueSureOrderBottomView.h"//底部结算按钮view xib 目前无法填坑,11-14已填好
#import "NewWSSureOrderCell.h" //11-23 复用
#import "WSSureOrderSectionHeader.h" //11-23 复用


//model
#import "CanPinMsgModel.h"
#import "WSSureOrderModel.h"
#import "AvaliableCouponModel.h"


#import "WSSureOrderControllerHelper.h" //工具类:请求数据 复用
#import "WSSureOrderTableViewProxy.h" //复用 tableView代理方法 复用g



//请求数据代理协议
@interface YuYueCustomView()<WSSureOrderControllerHelperDelegate>

/**1.tableView*/
@property (nonatomic, strong) UITableView *tableView;
/**表头*/
@property (nonatomic, strong) YuYueOrderHeaderView *headerView;
/**表尾*/
@property (nonatomic, strong) WSSureOrderFooterView *footerView;
/**底部结算view*/
@property (nonatomic, strong) YuYueSureOrderBottomView *bottomView;

/**3.订单信息模型*/
@property (nonatomic, strong) WSSureOrderModel *usefulModel;

//=======记录结算请求参数========
/**1.记录外界传过来的支付类型 0:在线支付, 1:到店支付*/
@property (nonatomic, strong) NSNumber *payType;
/**2.购物车id列表数组*/
@property (nonatomic, strong) NSArray *userCartIdList;
/**3.标记优惠券id、*/
@property (nonatomic, strong) NSNumber *couponId;
/**4.更新预约编号*/
@property (nonatomic, strong) NSNumber *yuyueBianHaoId;

///工具类
/**网络请求*/
@property (nonatomic, strong) WSSureOrderControllerHelper *networkHelper;
/**tableview代理方法抽取*/
@property (nonatomic, strong) WSSureOrderTableViewProxy *tvProxy;

/**1-11标记可用优惠券列表*/
@property (nonatomic, strong) NSArray *tempCouponListArr;

@end
//Class 'YuYueCustomView' does not conform to protocol 'WSSureOrderControllerHelperDelegate'
@implementation YuYueCustomView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView
{
    //第一部分:tableView
    [self addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = self.footerView;

    //第二部分:底部view
    [self addSubview:self.bottomView];
}


#pragma mark - layout
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.bottom.equalTo(self.bottomView.mas_top);
        make.top.equalTo(self.mas_top);
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(self.mas_bottom);
    }];
}

#pragma mark - 二 getter、
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        //代理
        _tableView.delegate = self.tvProxy;
        _tableView.dataSource = self.tvProxy;
        
        //需要在这里注册cell
        [_tableView registerClass:[NewWSSureOrderCell class] forCellReuseIdentifier:@"NewWSSureOrderCell"];
        [_tableView registerClass:[WSSureOrderSectionHeader class] forHeaderFooterViewReuseIdentifier:@"WSSureOrderSectionHeader"];
    }
    return _tableView;
}


- (YuYueOrderHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[YuYueOrderHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 322)]; //150 + 5 +167
    }
    return _headerView;
}


- (WSSureOrderFooterView *)footerView {
    if (!_footerView) {
        _footerView = [[WSSureOrderFooterView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
        _footerView.avaliableCouponLb.text = [NSString stringWithFormat:@"%@(0)",NSLocalizedString(Order_AvaliableCoupon, @"可用优惠券")];
        
        
        //2019-2-25客户说如果是预约vc就不显示预约提示语句
        _footerView.markTF.placeholder = @""; //✅
        
        FDWeakSelf;
        //可用优惠券
        _footerView.clickAvaliableCouponBlock = ^{
            if ([weakSelf.delegate respondsToSelector:@selector(clickAvaliableCouponWithArr:)]) {
                [weakSelf.delegate clickAvaliableCouponWithArr:weakSelf.tempCouponListArr];
            }
        };

        //显示支付方式
        _footerView.clickOnLineBlock = ^{
            weakSelf.payType = @(0); //在线
        };
        
        _footerView.clickOffLineBlock = ^{
            weakSelf.payType = @(1); //线下
        };
    }
    return _footerView;
}

//xibView
- (YuYueSureOrderBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[[NSBundle mainBundle] loadNibNamed:@"YuYueSureOrderBottomView" owner:self options:nil] firstObject];
        
        FDWeakSelf;
        //点击结算
        _bottomView.clickJieSuanBtnBlock = ^{
            [weakSelf clickJieSuanBtnAction];
        };
    }
    return _bottomView;
}


//网络请求工具类
- (WSSureOrderControllerHelper *)networkHelper {
    if (!_networkHelper) {
        _networkHelper = [[WSSureOrderControllerHelper alloc] init];
        //设置
        _networkHelper.delegate = self;
    }
    return _networkHelper;
}
//tableView代理方法
- (WSSureOrderTableViewProxy *)tvProxy {
    if (!_tvProxy) {
        _tvProxy = [[WSSureOrderTableViewProxy alloc] init];
        //设置代理
    }
    return _tvProxy;
}




- (void)clickJieSuanBtnAction {
    //1.判断请选择支付方式:防止支付方式为空导致奔溃
    if ([NSString isEmptyString:[NSString stringWithFormat:@"%@",self.payType]]) {
        [JRToast showWithText:NSLocalizedString(Order_SelectPayType, @"请选择支付方式")];
        return;
    }
    
    //2.组装请求参数
    NSDictionary *parDic = [self assembleJieSuanParDic];
    
    //3.调接口
    [self requestJieSuanJieKouWithParDic:parDic];
}

//!!!!:1-1 组装结算接口请求参数
- (NSDictionary *)assembleJieSuanParDic {
    //    NSString *deliveryType =
    NSNumber *deliveryType = @2; //1.预约点餐
    NSNumber *paymentType = self.payType;//2.支付方式 0:在线支付  1到店支付
    NSArray * userCartIdList = self.userCartIdList; //3.购物车idk列表
    NSNumber *userCouponId = self.couponId; //4.优惠券id
    NSNumber * userShopReservationId = self.yuyueBianHaoId; //5.门店预约编号
    NSString *remark = (self.footerView.markTF.text) ? : @""; //6.备注字段必须要有,有就是自己，没有就是 @""
    
    NSMutableDictionary *parM = [NSMutableDictionary dictionary];
    parM[@"deliveryType"] = deliveryType;
    parM[@"paymentType"] = paymentType;
    parM[@"userCartIdList"] = userCartIdList;
    if (userCouponId) { //有就设置,没有就忽略
        parM[@"userCouponId"] = userCouponId;
    }
    parM[@"userShopReservationId"] = userShopReservationId;
    parM[@"remark"] = remark;
    
    return parM;
}

//!!!!:1-2 结算接口网络请求
- (void)requestJieSuanJieKouWithParDic:(NSDictionary *)parDic {
    
    [MBProgressHUD fd_showHUDToView:self]; //菊花

    //请求
    [FDHttpAPi requestDDOrderSumitParDic:parDic successBlock:^(id responseObjc) {
        
        [MBProgressHUD fd_hideHUDFromView:self]; //移除菊花
        NSString *event = responseObjc[@"event"];
        
        BOOL isOk = NO; //假设失败
        
        if ([event isEqualToString:SUCCESS]) {
            isOk = YES; //成功
        }else {
            isOk = NO; //失败
            [JRToast showWithText:responseObjc[describe]];
        }
        //代理回调。
        if ([self.delegate respondsToSelector:@selector(clickJieSuanResult:)]) {
            [self.delegate clickJieSuanResult:isOk];
        }
        
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
        [MBProgressHUD fd_hideHUDFromView:self]; //移除菊花
    }];
}


#pragma mark - 四 网络请求
//!!!!:餐品信息相关数据
- (void)requestData {
    
    [self.networkHelper loadSureOrderData];
}


#pragma mark - WSSureOrderControllerHelperDelegate
//请求到数据
- (void)didRequestDataWithArr:(NSArray *)arr wsSureOrderModel:(WSSureOrderModel *)soModel userCouponList:(NSArray *)userCouponList {
    self.tvProxy.canPinMsgArrM = arr; //
    
    self.tempCouponListArr = userCouponList; //1-10
    
    [self.tableView reloadData];      //请求数据
}

- (void)didCountingCurrentSumPrice:(CGFloat)currentSumPrice originSumPrice:(CGFloat)originsumPrice {
//    FDLog(@"现价：%f 原价:%f",currentSumPrice,originsumPrice);
    //更新底部view
    [self.bottomView updateyuyueSureOrderBottomViewTotalPrice:currentSumPrice originPrice:originsumPrice];
}

//!!!!: 订单信息相关数据接口
- (void)requestYuYueShopDetailMsgWithYuYueId:(NSNumber *)yuyueId
{
    //备份门店预约编号:请求参数
    self.yuyueBianHaoId = yuyueId;
    
    [FDHttpAPi requestUserYuYueShopMesssageWithYuYueId:yuyueId successBlock:^(id responseObjc) {
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            NSDictionary *dataDic = responseObjc[@"data"];
            //01.店铺名
            NSString *shopName =dataDic[@"shop"][@"name"];
            //02.用餐时间
            NSNumber *diningTime = dataDic[@"diningTime"];
            NSString *timeStr = [NSString timeStrFromTimeStamp:[NSString stringWithFormat:@"%@",diningTime]];
            //05 地址
            NSString *address = dataDic[@"shop"][@"address"];
            
            
            //国际化
            NSString *contactPerson = NSLocalizedString(Home_contacts, @"联系人");
            NSString *contactPhone = NSLocalizedString(Order_ContactPhone, @"联系电话");
            NSString *consignee = [NSString stringWithFormat:@"%@    : %@",contactPerson,dataDic[@"consignee"]]; //03. 联系人
            NSString *phone = [NSString stringWithFormat:@"%@: %@",contactPhone,dataDic[@"phone"]];//04. 联系电话
    
            // 更新 表头数据
            [self.headerView updateShopName:shopName address:address timeStr:timeStr consignee:consignee phone:phone];

            
            //刷新数据
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }else{
            FDLog(@"%@",responseObjc[describe]);
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}


#pragma mark - 五 接口
/**3.提供一个接口更新可有优惠券金额*/
- (void)updateAvaliableCoupon:(AvaliableCouponModel *)couponModel {

    //不要了
//    CGFloat coupon = [couponModel.money floatValue];
//    self.footerView.couponPriceLb.text = [NSString stringWithFormat:@"%.1f",coupon];
    
    //记录优惠券id
    self.couponId = couponModel.couponId;
}




@end
